﻿using BrnMall.Web.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using BrnMall.Services;
using BrnMall.Web.MallAdmin.models;

namespace BrnMall.Web.MallAdmin.controllers
{
	public class WeiXinController : BaseMallAdminController
	{
		#region 接入设置

		[HttpGet]
		public ActionResult JoinConfig()
		{
			WebXinJoinConfigModel model = new WebXinJoinConfigModel();
			var info = AdminWeiXinService.GetJoinConfigInfo();
			if (info != null)
			{
				model.Id = info.Id;
				model.AppID = info.AppID;
				model.AppSecret = info.AppSecret;
				model.EncodingAESKey = info.EncodingAESKey;
				model.EncodingAESMode = info.EncodingAESMode;
				model.Token = info.Token;
				model.Url = info.Url;
			}
			return View(model);
		}
		[HttpPost]
		public ActionResult JoinConfig(WebXinJoinConfigModel model)
		{
			AdminWeiXinService.SaveJoinConfigInfo(new Core.Domain.WeiXin.JoinConfigInfo
			{
				Id = model.Id,
				Url = model.Url,
				AppID = model.AppID,
				AppSecret = model.AppSecret,
				Token = model.Token,
				EncodingAESKey = model.EncodingAESKey,
				EncodingAESMode = model.EncodingAESMode
			});
			return RedirectToAction("JoinConfig");
		}

		#endregion

		#region 菜单设置

		public ActionResult Menus()
		{
			var menus = AdminWeiXinService.GetWeiXinMenus();
			var modelList = new List<WeiXinMenuModel>();
			modelList.AddRange(menus.Where(_ => _.ParentId == 0).Select(_ => new WeiXinMenuModel
			{
				Id = _.Id,
				Name = _.Name,
				Url = _.Url,
				ParentId = _.ParentId
			}));

			foreach (var item in modelList)
			{
				item.Children = menus.Where(_ => _.ParentId == item.Id).Select(_ => new WeiXinMenuModel
				{
					Id = _.Id,
					Name = _.Name,
					Url = _.Url,
					ParentId = _.ParentId
				}).ToArray();
			}

			return View(modelList.ToArray());
		}

		#endregion
	}
}
